// @Time：2022/1/19 9:39
// @Software：GoLand
// @Author：JentZhang
package controller

import (
	"net/http"

	"github.com/gin-gonic/gin"

	"simple_oa/models"
	"simple_oa/utils"
)

// 身份验证
func Login(c *gin.Context) {
	var login models.Login
	err := c.ShouldBind(&login)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 2001,
			"msg":  "无效的参数",
		})
		return
	}
	// fmt.Println(user)
	// 检验用户名和密码是否正确
	if login.Username == "admin" && login.Password == "admin123456" {
		// 生成token
		tokenInfo, err := utils.GenToken(login.Username)
		if err != nil {
			c.JSON(http.StatusOK, gin.H{
				"code": 2001,
				"msg":  "生成token失败",
			})
			return
		}
		c.JSON(http.StatusOK, gin.H{
			"code": 200,
			"msg":  "success",
			"data": tokenInfo,
		})
		return
	} else {
		c.JSON(http.StatusOK, gin.H{
			"code": 2002,
			"msg":  "用户名或者密码错误",
		})
		return
	}
}
